package JeuInterfaces;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

public abstract class ChoixVersionAbstract extends JFrame{

	
	// les panels
	private JPanel contenaireJPanel, validationJPanel, selectionVersionJPanel;
	
	// la table de selection des versions
	private JTable selectionVersionJTable;
		
	// vecteur contenant les differentes versions
	protected Vector<Vector<String>> rowData;
	
	
	// les boutons
	private JButton validerBouton, annulerBouton;
	
	
	
	// les tailles.
	private Dimension choixVersionJFrameSize = new Dimension(400,240),
		validationJPanelDimension = new Dimension(395,45),
		selectionVersionDimension = new Dimension(395,150);
	
	
	public ChoixVersionAbstract(){		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		initialisationFenetre();
		initialisationComposants();
		inclusionsJPanels();
		this.setVisible(true);		
	}


	private void initialisationFenetre() {
		this.setSize(choixVersionJFrameSize);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setTitle("Carcassone : Choix de la version ");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	}

	private void initialisationComposants() {
		
		// liste les differentes versions du jeu
		initRowData();
		
		// les panels 
		contenaireJPanel = new JPanel();
		selectionVersionJPanel = new JPanel();
		validationJPanel = new JPanel();
		
		selectionVersionJPanel.setPreferredSize(selectionVersionDimension);
		validationJPanel.setPreferredSize(validationJPanelDimension);
		selectionVersionJPanel.setBorder(BorderFactory.createTitledBorder("Selection de la version"));
		validationJPanel.setBorder(BorderFactory.createTitledBorder(""));


		Vector<String> columnNames = new Vector<String>();

		
		columnNames.add("Version");
		selectionVersionJTable = new JTable(new MyModel(rowData, columnNames));
		selectionVersionJTable.setBackground(Color.BLACK);
		selectionVersionJTable.setPreferredSize(new Dimension(390,140));
		
		// les boutons
		validerBouton = new JButton("Valider");
		annulerBouton = new JButton("Annuler");
		validerBouton.addActionListener(new ValidationActionListener());
		annulerBouton.addActionListener(new AnnulerActionListener());
	}
	
	private void inclusionsJPanels() {
		this.setContentPane(contenaireJPanel);
		contenaireJPanel.add(selectionVersionJPanel);
		contenaireJPanel.add(validationJPanel);
		selectionVersionJPanel.add(selectionVersionJTable);
		validationJPanel.add(validerBouton);
		validationJPanel.add(annulerBouton);
	}
	
	
	class ValidationActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			int elementSelectionne = -1;
			try{
				elementSelectionne = selectionVersionJTable.getSelectedRow();
			}catch(Exception e){
				System.out.println("Exception");
			}
			if(elementSelectionne>-1){
				ChoixVersionAbstract.this.setVisible(false);
				choixPartie(rowData.elementAt(elementSelectionne).elementAt(0));
			}	
		}
	}	
	
	class AnnulerActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			ChoixVersionAbstract.this.setVisible(false);
		}		
	}
	
	//à redéfinir
	protected abstract void initRowData();
	protected abstract void choixPartie(String version);
}
